package com.huadi.firematerialident.service;

import com.huadi.firematerialident.dao.ReviewerDaoImpl;
import com.huadi.firematerialident.dao.UserDaoImpl;
import com.huadi.firematerialident.po.Admin;
import com.huadi.firematerialident.po.Reviewer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ReviewerServiceImpl implements ReviewerService{
    @Autowired
    ReviewerDaoImpl reviewerDao;
    @Autowired
    UserDaoImpl userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addReviewer(Reviewer reviewer) {
        return reviewerDao.addReviewer(reviewer);
    }

    @Override
    public int deleteReviewerByID(String revID) {
        return reviewerDao.deleteReviewerByID(revID);
    }

    @Override
    public int updateReviewer(Reviewer reviewer) {
        return reviewerDao.updateReviewer(reviewer);
    }

    @Override
    public List<Map<String,Object>> selectReviewerByID(String revID) {
        return reviewerDao.selectReviewerByID(revID);
    }

    @Override
    public List<Map<String,Object>> getAllReviewer() {
        return getAllReviewer();
    }

    @Override
    public String getRevID(){
        String RevCount = userDao.getUsername();
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select RevID,RevCount from reviewer");
        for(int i=0;i<list.size();i++) {
            if (RevCount.equals(list.get(i).get("RevCount"))) {
                return (String) list.get(i).get("RevID");
            }
        }
        return null;
    }

    @Override
    public Reviewer selectReviewerByID_class(String id){
        List<Map<String ,Object>> list = reviewerDao.selectReviewerByID(id);
        Reviewer reviewer = new Reviewer();
        reviewer.setRevID((String) list.get(0).get("RevID"));
        reviewer.setRevCount((String) list.get(0).get("RevCount"));
        reviewer.setRevPassword((String) list.get(0).get("RevPassword"));
        reviewer.setRevName((String) list.get(0).get("RevName"));
        reviewer.setRevSex((String) list.get(0).get("RevSex"));
        reviewer.setRevBirth((LocalDate) list.get(0).get("RevBirth"));
        reviewer.setRevPhone((String) list.get(0).get("RevPhone"));
        reviewer.setLogicalAddDel((boolean) list.get(0).get("LogicalAddDel"));
        reviewer.setLastModification((LocalDateTime) list.get(0).get("LastModification"));
        return reviewer;
    }
}
